package pe.com.cotelasa.scp.dominio.common.util;

import java.io.UnsupportedEncodingException;
import java.text.DecimalFormat;
import java.util.Random;

public class UtilitarioCadenas {

    public static String rPad(String s, int total, char c) {
        int longitud = s.length();
        if (longitud < total) {
            int falta = total - longitud;
            for (int i = 0; i < falta; i++) {
                s += c;
            }
        }
        return s;
    }

    /**
     * Concatena caracteres por la izquierda
     */
    public static String lPad(String s, int total, char c) {
        int longitud = s.length();
        String cadena1 = "";
        if (longitud < total) {
            int falta = total - longitud;
            for (int i = 0; i < falta; i++) {
                cadena1 += c;
            }
        }
        cadena1 += s;
        return cadena1;
    }

    /**
     * Elimina caracteres encontrados
     */
    public static String eliminarCaracter(String cad, char car) {
        String s = "";
        char c;
        for (int i = 0; i < cad.length(); i++) {
            c = cad.charAt(i);
            if (c != car) {
                s += c;
            }
        }
        return s;
    }

    /**
     * Retorna una l�nea de caracteres de longitud especificada.
     */
    public static String formarCadena(char c, int n) {
        char x[] = new char[n];
        if (n <= 0) {
            return "";
        }
        int j = 0;
        while (j < n) {
            x[j] = c;
            j++;
        }
        String s = new String(x);
        return s;
    }

    public static String formatearCadena(String s) {
        s = s.trim();
        s = s.replace("  ", " ");
        return s;
    }

    public static String formatoNumeroDocumento(int numero) {
        String format = "%1$08d";
        return String.format(format, numero);
    }

    public static String formatoSerieDocumento(int numero) {
        String format = "%1$03d";
        return String.format(format, numero);
    }

    /**
     * Retorna una cadena de alfanumerica aleatoria.
     */
    public static String generadorCadenaAleatorio() {
        String cadenaAleatoria = "";
        long milis = new java.util.GregorianCalendar().getTimeInMillis();
        Random r = new Random(milis);
        int i = 0;
        while (i < 6) {
            char c = (char) r.nextInt(255);
            if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z')) {
                cadenaAleatoria += c;
                i++;
            }
        }
        return cadenaAleatoria;
    }

    public static boolean isEmpty(String string) {
        return (string == null || string.trim().equals(""));
    }

    public static String generateAuthCode() {
        System.out.println("Entering generateAuthCode()");
        //getting the current time in nanoseconds
        long decimalNumber = System.nanoTime();
        System.out.println("current time in nanoseconds: " + decimalNumber);

        //To convert time stamp to alphanumeric code.
        //We need to convert base10(decimal) to base36
        String strBaseDigits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        String strTempVal = "";
        int mod = 0;
        int authCodeLength = 0;
        // String concat is costly, instead we could have use stringbuffer or stringbuilder
        //but here it wont make much difference.
        while (decimalNumber != 0 && authCodeLength < 8) {
            mod = (int) (decimalNumber % 36);
            strTempVal = strBaseDigits.substring(mod, mod + 1) + strTempVal;
            decimalNumber = decimalNumber / 36;
            authCodeLength++;
        }
        System.out.println("alphanumeric code generated from TimeStamp : " + strTempVal);
        return strTempVal;

    }

    public static String formatearNumero(Object o) {
        DecimalFormat df = new DecimalFormat("#,###.##");
        String s = df.format(o);
        return s;
    }

    public static String extraerSubcadenaExacta(String cadena, int num) {

        if (cadena.length() > 0 && cadena.length() != num) {

            if (cadena.length() > num) {
                cadena = cadena.substring(0, num);
                if (cadena.lastIndexOf(" ") != (cadena.length() - 1)) {
                    String[] cadenas = cadena.split(" ");
                    cadena = cadenas[0];
                    for (int i = 1; i < cadenas.length; i++) {
                        cadena += " " + cadenas[i];
                    }

                }


            } else {
                cadena = cadena.substring(0, cadena.length());
            }

        }

        return cadena;
    }
    
    public static String convertirUTF8(String cadena) throws UnsupportedEncodingException{
        return (new String(cadena.getBytes("iso-8859-1"), "UTF-8"));
    }
}
